//
//  VideoPlayView.swift
//  Bilibili
//
//  Created by MBA on 2020/10/2.
//  Copyright © 2020 MBA. All rights reserved.
//

import SwiftUI
import AVFoundation
struct VideoPlayView: View {
    
    @State var isPlay:Bool=true
    @State var isloding:Bool=false
    
    var body: some View {
        
        ZStack {
            Color(.black)
                .edgesIgnoringSafeArea(.all)
            
            
            PlayerView()
            
            
            VStack {
                VideoHeadView()
                    .padding(.horizontal)
                Spacer()
                HStack(alignment: .bottom) {
                    VideoNameView()
                        .padding(/*@START_MENU_TOKEN@*/.all/*@END_MENU_TOKEN@*/)
                    Spacer()
                    viedeToolView()
                        .padding(/*@START_MENU_TOKEN@*/.all/*@END_MENU_TOKEN@*/)
                }
            }
            .shadow(color: Color.black.opacity(0.3), radius: 4, x: 4, y: 4)
            .shadow(color: Color.white.opacity(0.3), radius: 4, x: 4, y: 4)
            
            .foregroundColor(.white)
            
            
            
        }
        
        
    }
}

struct VideoPlayView_Previews: PreviewProvider {
    static var previews: some View {
        VideoPlayView()
    }
}

struct VideoNameView: View {
    var body: some View {
        
        VStack(alignment: .leading) {
            HStack {
                Text("@Hello, World!")
                    .font(.system(size: 18, weight: .bold))
                Text("9月12日")
                    .foregroundColor(Color("info"))
            }
            Text("Design and animate a high converting landing page with advanced interactions, payments and CMS")
                .lineLimit(/*@START_MENU_TOKEN@*/2/*@END_MENU_TOKEN@*/)
            
            HStack {
                Image(systemName:"waveform.path.ecg")
                    .foregroundColor(Color.white)
                    .frame(width: 16.0, height: 16.0)
                Text("伤心太平洋")
            }
        }
        
    }
    
}

struct viedeToolView: View {
    var body: some View {
        
        VStack(alignment: .center){
            ZStack {
                Image(systemName:"ladybug.fill")
                    .resizable()
                    .aspectRatio(contentMode: .fit)
                    .padding(.bottom)
                
                VStack {
                    Spacer()
                    Image(systemName:"plus.circle")
                        .resizable()
                        
                        .frame(width: 24.0, height: 24.0)
                        .background(Color.red)
                        .clipShape(Circle())
                }
                
            }
            .padding(.bottom)
            .frame(width: 50, height: 80)
            
            
            VStack{
                
                Image(systemName:"suit.heart.fill")
                    .font(.system(size: 32))
                    .frame(width: 52.0, height: 52.0)
                Text("100")
            }
            .padding(.bottom)
            
            VStack{
                
                Image(systemName:"message.fill")
                    .font(.system(size: 32))
                    .frame(width: 52.0, height: 52.0)
                Text("100")
            }
            .padding(.bottom)
            
            VStack{
                
                Image(systemName:"paperplane.fill")
                    .font(.system(size: 32))
                    .frame(width: 52.0, height: 52.0)
                Text("100")
            }
            .padding(.bottom)
            
            
            Image(systemName:"sparkles")
                .padding(.bottom)
                .font(.system(size: 32))
                .frame(width: 60.0, height: 60.0)
                .background(Color.black.opacity(0.4))
                .clipShape(Circle())
                .shadow(color: Color.black.opacity(0.3), radius: 20, x: 0, y: 20)
            
            
        }
        .foregroundColor(Color.white)
        
        
    }
    
    
}

struct VideoHeadView: View {
    @Environment(\.presentationMode) var presentationMode
    var body: some View {
        VStack {
            HStack {
                
                Image(systemName: "xmark")
                    .frame(width: 36, height: 36)
                    .foregroundColor(.white)
                    .background(Color.black.opacity(0.5))
                    .clipShape(Circle())
                    .onTapGesture {
                        self.presentationMode.wrappedValue.dismiss()
                    }
                Spacer()
                Image(systemName: "magnifyingglass")
                    .resizable()
                    .frame(width: 32, height: 32)
                    .foregroundColor(.white)
                    .clipShape(Circle())
                    .onTapGesture {
                        
                    }
            }
        }
    }
}

struct PlayerView: View {
    
    @State var videoPos: Double=0
    @State var videoDuration: Double=0
    @State var seeking: Bool=false
    @State var isPlay:Bool=true
    @State var status:AVPlayer.TimeControlStatus = AVPlayer.TimeControlStatus.paused

    
    private let player: AVPlayer = AVPlayer(url: URL(string: "https://bitdash-a.akamaihd.net/content/sintel/hls/playlist.m3u8")!)
    
    
    
    private func pausePlayer(_ pause: Bool) {
        isPlay = pause
        
        if isPlay {
            player.pause()
        }
        else {
            player.play()
        }
    }
    
    private func sliderEditingChanged(editingStarted: Bool) {
        if editingStarted {
            
            seeking = true
            pausePlayer(true)
        }
        
        // Do the seek if we're finished
        if !editingStarted {
            let targetTime = CMTime(seconds: videoPos * videoDuration,
                                    preferredTimescale: 600)
            player.seek(to: targetTime) { _ in
           
                self.seeking = false
                self.pausePlayer(false)
            }
        }
    }
    var body: some View {
        ZStack {
            
            VideoPlayerView(videoPos: $videoPos,
                            videoDuration: $videoDuration,
                            seeking: $seeking,
                            status: $status,
                            player: player
                         
            )
            if status == .paused{
                Image(systemName:"play.circle")
                    .resizable()
                    
                    .frame(width: 60, height: 60)
                    .foregroundColor(.white)
                    .background(Color.black.opacity(0.5))
                    .clipShape(Circle())
            }
            if status == .waitingToPlayAtSpecifiedRate{
                LoadingView()
                    .background(Color.black.opacity(0.3))
                    .edgesIgnoringSafeArea(.all)
            }
            if status == .playing {

            }
        
            
            
            
        }
        .onAppear(){
            
        }
        .onDisappear {
            //            self.player.replaceCurrentItem(with: nil)
        }
        .onTapGesture {
            isPlay.toggle()
            pausePlayer(isPlay)
        }
        
    }
}
